package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Ads;
import ex.com.jm.food.entity.Company;
import ex.com.jm.food.entity.CompanyDiscount;
import ex.com.jm.food.entity.CompanyType;
import ex.com.jm.food.service.UserAdsService;
import ex.com.jm.food.service.UserComDisService;
import ex.com.jm.food.service.UserCompanyService;
import ex.com.jm.food.service.UserCompanyTypeService;
import ex.com.jm.food.utils.JsonUtils;

/**
 * 整个饿9定主页action
 * @author zhaojw
 *
 */
public class UserIndexAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private UserAdsService userAdsService;
	private UserCompanyTypeService userCompanyTypeService;
	private UserComDisService userComDisService;
	private UserCompanyService userCompanyService;
	private List<Ads> ads=new ArrayList<Ads>();
	private List<CompanyType> companyTypes=new ArrayList<CompanyType>();
	private List<CompanyDiscount> companyDiscounts=new ArrayList<CompanyDiscount>();
	private String address="郑州";
	private String comName;
	private Integer comType;
	private Integer comDistr;
	private String comDiscount;
	private int initPage=1;
	private int pageRow=30;
	private int totalCount;
	private String order;
	private String sort;
	private String comId;
	public UserAdsService getUserAdsService() {
		return userAdsService;
	}
	public void setUserAdsService(UserAdsService userAdsService) {
		this.userAdsService = userAdsService;
	}
	public UserCompanyTypeService getUserCompanyTypeService() {
		return userCompanyTypeService;
	}
	public void setUserCompanyTypeService(
			UserCompanyTypeService userCompanyTypeService) {
		this.userCompanyTypeService = userCompanyTypeService;
	}
	public UserComDisService getUserComDisService() {
		return userComDisService;
	}
	public void setUserComDisService(UserComDisService userComDisService) {
		this.userComDisService = userComDisService;
	}
	public UserCompanyService getUserCompanyService() {
		return userCompanyService;
	}
	public void setUserCompanyService(UserCompanyService userCompanyService) {
		this.userCompanyService = userCompanyService;
	}
	public List<Ads> getAds() {
		return ads;
	}
	public void setAds(List<Ads> ads) {
		this.ads = ads;
	}
	public List<CompanyType> getCompanyTypes() {
		return companyTypes;
	}
	public void setCompanyTypes(List<CompanyType> companyTypes) {
		this.companyTypes = companyTypes;
	}
	public List<CompanyDiscount> getCompanyDiscounts() {
		return companyDiscounts;
	}
	public void setCompanyDiscounts(List<CompanyDiscount> companyDiscounts) {
		this.companyDiscounts = companyDiscounts;
	}
    
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getComName() {
		return comName;
	}
	public void setComName(String comName) {
		this.comName = comName;
	}
	public Integer getComType() {
		return comType;
	}
	public void setComType(Integer comType) {
		this.comType = comType;
	}
	public Integer getComDistr() {
		return comDistr;
	}
	public void setComDistr(Integer comDistr) {
		this.comDistr = comDistr;
	}
	public String getComDiscount() {
		return comDiscount;
	}
	public void setComDiscount(String comDiscount) {
		this.comDiscount = comDiscount;
	}
	
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getComId() {
		return comId;
	}
	public void setComId(String comId) {
		this.comId = comId;
	}
	/**
	 * 到达首页
	 * @return
	 */
	public String userIndex(){
		ads=this.getUserAdsService().getAds();
		companyTypes=this.getUserCompanyTypeService().getCompanyTypes();
		companyDiscounts=this.getUserComDisService().getCompanyDiscounts();
		System.err.println(companyTypes.toString());
		return "userIndex";
		
	}
	/**
	 * 获取数据
	 */
	public void getCompanys(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		System.out.println(comDistr+"=====>>"+comDistr);
		List<Company> companies=this.getUserCompanyService().getCompanies(offset, pageRow, address, comName, comType, comDistr, comDiscount, order, sort);
		JSONArray jocom=JSONArray.fromObject(companies);
		totalCount=this.getUserCompanyService().comCount(address, comName, comType, comDistr, comDiscount);
		JSONObject jo=JsonUtils.buildJson(jocom, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jo);
		out.print(jo);
		out.flush();
		out.close();
	}
	/**
	 * 获取某一个企业信息
	 */
	public void getCompany(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		List<Company> company=this.getUserCompanyService().getCompany(comId);
		JSONArray jo=JSONArray.fromObject(company);
		System.out.println(jo);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jo);
		out.print(jo);
		out.flush();
		out.close();
	}
}
